/**
 * @file dev_key.c
 * @brief
 * @author He Shaohua (hsh15529870639@foxmail.com)
 * @date 2024-05-23
 * @version 1.0
 * @copyright Copyright (c) 2024, SNUT-Chaoyue Team
 * ****************************************************************************************************
 * @par Change Log:
 * <table>
 * <tr><th>Date         <th>Version       <th>Author      <th>Description
 * <tr><td>2024-05-23   <td> 1.0          <td>He Shaohua  <td>Creator
 *
 * </table>
 * ==================================----How to use this driver----=====================================
 * [..]
 * @note
 *   -#
 *
 * @warning
 *   -#
 *
 * *****************************************************************************************************
 * @attention
 *
 * This document is required and please understand any bugs that may occur in the document.
 * If you want to modify this file, after the code update is completed, update the version number in time,
 * indicate the update date, and fill in the changes
 * Make sure that the submitted code does not have many bugs.
 *
 * All rights reserved.
 * *****************************************************************************************************
 */
#include "dev_encoder.h"

uint32_t gpio_interrup;
static int32_t Get_Encoder_L_count = 0;
static int32_t Get_Encoder_R_count = 0;
float Speed_L = 0.0f;
float Speed_R = 0.0f;
void GROUP1_IRQHandler(void)
{
	gpio_interrup = DL_GPIO_getEnabledInterruptStatus(GPIOA, ENCODER_L_L_A_PIN | ENCODER_L_L_B_PIN | ENCODR_R_R_A_PIN | ENCODR_R_R_B_PIN);
	// encoderA
	if ((gpio_interrup & ENCODER_L_L_A_PIN) == ENCODER_L_L_A_PIN)
	{
		if (!DL_GPIO_readPins(GPIOA, ENCODER_L_L_B_PIN))
		{
			Get_Encoder_L_count--;
		}
		else
		{
			Get_Encoder_L_count++;
		}
	}
	else if ((gpio_interrup & ENCODER_L_L_B_PIN) == ENCODER_L_L_B_PIN)
	{
		if (!DL_GPIO_readPins(GPIOA, ENCODER_L_L_A_PIN))
		{
			Get_Encoder_L_count++;
		}
		else
		{
			Get_Encoder_L_count--;
		}
	}

	// encoderB
	if ((gpio_interrup & ENCODR_R_R_A_PIN) == ENCODR_R_R_A_PIN)
	{
		if (!DL_GPIO_readPins(GPIOA, ENCODR_R_R_B_PIN))
		{
			Get_Encoder_R_count--;
		}
		else
		{
			Get_Encoder_R_count++;
		}
	}
	else if ((gpio_interrup & ENCODR_R_R_B_PIN) == ENCODR_R_R_B_PIN)
	{
		if (!DL_GPIO_readPins(GPIOA, ENCODR_R_R_A_PIN))
		{
			Get_Encoder_R_count++;
		}
		else
		{
			Get_Encoder_R_count--;
		}
	}

	DL_GPIO_clearInterruptStatus(GPIOA, ENCODER_L_L_A_PIN | ENCODER_L_L_B_PIN | ENCODR_R_R_A_PIN | ENCODR_R_R_B_PIN);
}

int32_t V_L_Measure(void)
{
	int32_t temp;
	temp = Get_Encoder_L_count;
	Get_Encoder_L_count = 0;

	return temp;
}

float Motor_L_Velocity(void)
{
	int32_t num;
	float Rev_L = 0.0f;
	num = V_L_Measure();
	Rev_L = (float)num * C;
	return Rev_L;
}

int32_t V_R_Measure(void)
{
	int32_t temp;
	temp = Get_Encoder_R_count;
	Get_Encoder_R_count = 0;

	return temp;
}

float Motor_R_Velocity(void)
{
	int32_t num;
	float Rev_L = 0.0f;
	num = V_R_Measure();
	Rev_L = (float)num * C;
	return Rev_L;
}

void TIMER_0_INST_IRQHandler(void)
{
	if (DL_TimerA_getPendingInterrupt(TIMER_0_INST))
	{
		if (DL_TIMER_IIDX_ZERO)
		{
			Speed_L = Motor_L_Velocity();
			Speed_R = Motor_R_Velocity();
		}
	}
}
